package com.yanq.liancar.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.bean.VerificationCodeBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.CountDownTimerUtils;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FindPwdStepOneActivity extends AppCompatActivity {
    private static final String TAG = "FindPwdStepOneActivity";

    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.gang)
    TextView gang;
    @BindView(R.id.phone_num)
    EditText phoneNum;
    @BindView(R.id.sms)
    EditText sms;
    @BindView(R.id.btn_get_yzm)
    Button btnGetYzm;
    @BindView(R.id.next_step)
    TextView nextStep;

    private String code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setStatusBar(this, false, false);
        setContentView(R.layout.activity_find_pwd_step_one);
        ButterKnife.bind(this);
        initViewsAndEvents();
    }


    @OnClick({R.id.back_btn, R.id.btn_get_yzm,R.id.next_step})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.btn_get_yzm:
                if (Utils.isMobile(phoneNum.getText().toString().trim())){
                    getRandomNum();
                }else {
                    RxToast.error("请输入正确手机号");
                }
                break;
            case R.id.next_step:
                if (!TextUtils.isEmpty(sms.getText().toString().trim())){
                    if (sms.getText().toString().trim().equals(code)){
                        Bundle bundle=new Bundle();
                        bundle.putString("phone",phoneNum.getText().toString().trim());
                        RxActivityTool.skipActivity(this,FindPwdStepTwoActivity.class,bundle );
                    }else {
                        RxToast.error("验证码有误");
                    }
                }else {
                    RxToast.error("请输入验证码");
                }
                break;
        }
    }

    //获取验证码
    private void getRandomNum() {
        OkGo.<String>post(Config.s + "llc/getCode")
                .tag(this)
                .params("tel", phoneNum.getText().toString().trim())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson=new Gson();
                        VerificationCodeBean bean=gson.fromJson(response.body(),VerificationCodeBean.class);
                        if (bean.isIsSuccess()){
                            code=bean.getData();
                            CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(btnGetYzm, 60000, 1000);
                            countDownTimerUtils.start();
                            RxToast.success(bean.getOutputMessage());
                        }else {
                            RxToast.error(bean.getOutputMessage());
                        }

                    }
                });
    }

    private void initViewsAndEvents() {
        phoneNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                LogUtils.e("1111111111",i+"");
//                if (i > 9 ) {
//                    btnGetYzm.setEnabled(true);
//                } else {
//                    btnGetYzm.setEnabled(false);
//                }
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                Log.d(TAG, "afterTextChanged 被执行---->" + editable);
                if (Utils.isMobile(editable.toString())){
                    btnGetYzm.setEnabled(true);
                }else {
                    btnGetYzm.setEnabled(false);
                }
            }
        });
    }

}
